package ru.housewife.form;

import ru.housewife.model.EatingMenu;
import ru.housewife.model.EatingType;
import ru.housewife.model.UsersTable;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author : atroshanin
 */
public class DayMenuWebWrapperForm {

    private final UsersTableForm usersTable;

    private final DayMenuForm dayMenu;

    private final int dayNum;

    private final boolean activePeriod;

    private final boolean today;

    private final Date date;

    private final int dayOfWeek;

    public DayMenuWebWrapperForm(UsersTableForm usersTable, DayMenuForm dayMenu, int dayNum, boolean activePeriod, Calendar c) {
        this(usersTable, dayMenu, dayNum, activePeriod, false, c);
    }

    public DayMenuWebWrapperForm(UsersTableForm usersTable, DayMenuForm dayMenu, int dayNum, boolean activePeriod, boolean today, Calendar c) {
        this.usersTable = usersTable;
        this.dayMenu = dayMenu;
        this.dayNum = dayNum;
        this.activePeriod = activePeriod;
        this.today = today;
        this.date = c.getTime();
        this.dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
    }

    public DayMenuForm getDayMenu() {
        return dayMenu;
    }

    public boolean isActivePeriod() {
        return activePeriod;
    }

    public Date getDate() {
        return date;
    }

    public int getDayOfWeek() {
        return dayOfWeek;
    }

    public int getDayOfWeekByDayNum() {
        return dayNum == 6 ? 0 : dayNum + 2;
    }

    public int getEatingsCount() {
        return dayMenu != null ? dayMenu.getEatingMenus().size() : 0;
    }

    public int getDayNum() {
        return dayNum + 1;
    }

    public boolean isWeekType() {
        return usersTable.getType() == UsersTable.Type.WEEK_7;
    }

    public long getUserTableId() {
        return usersTable.getId();
    }

    public String getDayMenuCssClass() {
        String cssClass = today ? "today_daymenu " : "";
        if (dayMenu != null) {
            if (today) {
                return cssClass + "cur_active_daymenu";
            }
            return activePeriod ? cssClass + "active_daymenu" : cssClass + "inactive_daymenu";
        }
        return cssClass + "no_daymenu";
    }

    public long getDayMenuId() {
        return dayMenu != null ? dayMenu.getId() : 0;
    }

    public List<GroupedEatingMenus> getEatingMenusGroups() {
        List<GroupedEatingMenus> result = new ArrayList<GroupedEatingMenus>();

        EatingType curType = null;
        List<EatingMenuForm> sortedEmfs = dayMenu.getEatingMenus();
        int curTypeStartIndx = -1;
        int curIndex = 0;
        for (EatingMenuForm emf : sortedEmfs) {
            if (emf.getEatingType() != curType) {
                if (curType != null) {
                    result.add(new GroupedEatingMenus(curType, sortedEmfs.subList(curTypeStartIndx, curIndex)));
                }
                curType = emf.getEatingType();
                curTypeStartIndx = curIndex;
            }
            curIndex++;
        }
        if (curType != null) {
            result.add(new GroupedEatingMenus(curType, sortedEmfs.subList(curTypeStartIndx, curIndex)));
        }
        return result;
    }

    public class GroupedEatingMenus {
        private EatingType type;
        private List<EatingMenuForm> eatingMenus;

        GroupedEatingMenus(EatingType type, List<EatingMenuForm> eatingMenus) {
            this.type = type;
            this.eatingMenus = eatingMenus;
        }

        public EatingType getType() {
            return type;
        }

        public List<EatingMenuForm> getEatingMenus() {
            return eatingMenus;
        }
    }
}
